package com.lancoo.answer.ui.ques;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.model.eventBean.AnswerSheetEventBean;
import com.lancoo.answer.ui.child.training.ChildWordListenWriteAnswerFragment;
import com.lancoo.answer.util.paperUtils.FragmentCommunicationUtils;
import com.lancoo.answer.util.paperUtils.FragmentLoadUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by pengmi on 2021/12/3 16:23
 * Description:上下不分页layout fragment
 */
public class NoSpliteLayoutFragment extends Fragment {

    private Child child;
    private View inflate;
    private static final String TAG = "NoSpliteLayoutFragment";
    private int quesIndex;
    private int typeIndex;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.ev_fragment_not_splite_layout, container, false);
        return inflate;

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    public static Fragment loadFragment(int typeIndex,int queseIndex,int defaultLoadIndex){
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,typeIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,queseIndex);
        mBundle.putInt(AnswerConstant.KEY_LOADCHILDINDEX,defaultLoadIndex);
        NoSpliteLayoutFragment noSpliteLayoutFragment=new NoSpliteLayoutFragment();
        noSpliteLayoutFragment.setArguments(mBundle);
        return noSpliteLayoutFragment;
    }

    private void initView(View view){
        if(getArguments()==null){
            return;
        }
        if(ConstantBean.Companion.getConstantExamBean()==null){
            return;
        }
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        typeIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        quesIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        if(kindBeanListk==null){
            return;
        }
        List<Ques> quesList = kindBeanListk.get(typeIndex).getQuesList();
        if( quesList==null||
                quesList.size()<= quesIndex){
            return;
        }
        Ques ques = quesList.get(quesIndex);
        List<Child> childList = ques.getChildList();
        if(childList==null){
            return;
        }
        child = childList.get(0);
        FragmentManager childFragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
        Fragment fragment= FragmentLoadUtils.loadChildFragment(ques, typeIndex, quesIndex,0,0,0,0);
        Log.e(TAG, "initView: fragment name:" +ques.getGenreName()+".."+ques.getGenreID());
        fragmentTransaction.replace(R.id.framelayout,fragment,"123");
        fragmentTransaction.commitAllowingStateLoss();
    }

    public void onTrainStateChange(){
        Fragment fragment = getChildFragmentManager().findFragmentByTag("123");
        if(fragment instanceof ChildWordListenWriteAnswerFragment){
            ((ChildWordListenWriteAnswerFragment) fragment).onTrainEnable();
        }
    }

    public void audioPlayAudio(){
        Fragment fragment = getChildFragmentManager().findFragmentByTag("123");
        if(fragment instanceof ChildWordListenWriteAnswerFragment){
            ((ChildWordListenWriteAnswerFragment) fragment).autoPlayAudio();
        }
    }

    /**
     * 跳转到指定小题 ，指定答题点
     * @param childIndex 小题序号
     * @param itemIndex 答题点序号
     */
    public void jumpToAimChildFragment(int quesIndex,int childIndex,int itemIndex){
        Fragment fragment = getChildFragmentManager().findFragmentByTag("123");
        FragmentCommunicationUtils.jumpToItemFragment(fragment,childIndex,itemIndex);
    }

    public void markQuestion(){
        Fragment fragment = getChildFragmentManager().findFragmentByTag("123");
        FragmentCommunicationUtils.markQuestion(fragment);
    }



}
